#!/usr/bin/python
#########################################################################
# Author: Kai Ren
# Created Time: 2012-01-24 00:19:28
# File Name: ./check.py
# Description: 
#########################################################################
import matplotlib.pyplot as plt 
import math
import numpy as np

def distinct_vals(va):
    a = {}
    for vi in va:
        nkey = "%.2f"%(vi)
        if not nkey in a:
            a[nkey] = 0.0
        a[nkey] += 1
    n = len(va)
    entropy = 0.0
    for ai in a:
        p = a[ai] / n
        entropy += p * math.log(p)
    if len(a) > 80:
       print max(a.values())
    return (len(a), -entropy)

def linear_predictor(val, step):
    y = np.matrix([val[step:]]).getT()
    x = []
    for i in range(len(val)-step):
        xi = []
        for j in range(step):
            xi.append(val[i+j])
        x.append(xi)
    x = np.matrix(x)
    p = x.getT() * x
    beta = p.getI() * x.getT() * y
    return beta

def linear_delta(val, beta, step):
    y = []
    for i in range(len(val)-step):
        elem = 0
        for j in range(step):
            elem += val[i+j] * beta[j][0]
        y.append(val[i+step] - elem)
    print sum(y)
    return y

def low_pass_filter(x, dt=30):
    lof_m = 60
    rc = dt * lof_m / math.pi
    alpha = dt / (rc + dt)
    y = [0] * len(x)
    y[0] = x[0]
    z = []
    for i in range(1, len(x)):
        y[i] = alpha * x[i] + (1 - alpha) * y[i-1]
        z.append(y[i] - x[i])
    return z

val = np.load('./data/val3.npy')

for step in [1, 2, 3, 4, 5]:
    beta = linear_predictor(val, step)
    print beta
    print distinct_vals(linear_delta(val, beta, step))

